//
//  HMHomeTableViewController.swift
//  Weibo
//
//  Created by ZhuDong on 16/5/19.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SVProgressHUD
private let HomeCellID = "HMHomeViewCell"
class HMHomeTableViewController: HMTableViewController {
    
    lazy var listViewModel: HMListViewModel = HMListViewModel()
    lazy var refreshView: HMRefreshControl = {
        let re = HMRefreshControl()
        re.addTarget(self, action: #selector(HMHomeTableViewController.loadData), forControlEvents: .ValueChanged)
        return re
    }()
    lazy var pullingView: UIActivityIndicatorView = {
        let v = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
//            v.startAnimating()
            return v
    }()
    lazy var tipLabel: UILabel = {
       let l = UILabel(frame: CGRectMake(0, -20, ScreenWidth, 44))
        l.textColor = UIColor.whiteColor()
        l.font = UIFont.systemFontOfSize(14);
        l.backgroundColor = UIColor.orangeColor()
        l.textAlignment = .Center
        return l
    }()
    override func viewDidLoad() {
        super.viewDidLoad()
        setNav()
        if !view.isKindOfClass(UITableView.self) {
           visitorView.updateUI(true)
            return
        }
        prepareForTableView()
        loadData()
    }
    private func prepareForTableView(){
        rigisterCell()
        tableView.estimatedRowHeight = 300
        tableView.addSubview(refreshView)
        tableView.tableFooterView = pullingView
    }
    private func rigisterCell(){
        tableView.registerClass(HMHomeViewCell.self, forCellReuseIdentifier: HomeCellID)
    }
    @objc private func loadData(){
        listViewModel.loadData(pullingView.isAnimating()) {  (isSuccess,count) in
//            self.refreshControl?.endRefreshing()
            self.pullingView.stopAnimating()
            self.refreshView.endRefreshing()
            if !isSuccess{
                SVProgressHUD.showErrorWithStatus(errorStrOfNet)
                return
            }
            self.tipLabel.text = count == 0 ? "没有新数据":"有\(count)条新微博";
            let rect = self.tipLabel.frame
            UIView.animateWithDuration(0.25, animations: { 
                self.tipLabel.frame = CGRectMake(0, 44, ScreenWidth, self.tipLabel.frame.size.height)
                }, completion: { (_) in
                    UIView.animateWithDuration(0.25, delay: 1, options: [], animations: { 
                        self.tipLabel.frame = rect
                        }, completion: nil)
            })
            self.tableView.reloadData()
        }
        
    }
    func setNav(){
        let rightBtn = UIBarButtonItem(imageName: "navigationbar_pop", target: self, action: #selector(rightBtnClick))
        navigationItem.rightBarButtonItem = rightBtn
//        self.navigationController?.navigationBar.addSubview(tipLabel)
        self.navigationController?.navigationBar.insertSubview(tipLabel, atIndex: 0)
    }
    
    func rightBtnClick() {
        let detail = HMDetailViewController()
        detail.hidesBottomBarWhenPushed = true
        detail.view.backgroundColor = UIColor.whiteColor()
        navigationController?.pushViewController(detail, animated: true)
        
    }
    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.listViewModel.statusesViewModels.count
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        //
        let cell = tableView.dequeueReusableCellWithIdentifier(HomeCellID) as! HMHomeViewCell
        cell.statusesViewModel = self.listViewModel.statusesViewModels[indexPath.row]
        if indexPath.row == self.listViewModel.statusesViewModels.count - 1 && !pullingView.isAnimating(){
            pullingView.startAnimating()
            loadData()
        }
        return cell
    }

   }
